import { getRandomDateTime, getNowDate, getFeatureDate } from '../../../utils'
import Mock from 'mockjs'
export const resourceLibraryFileMock = pageSize => {
  const result: any[] = []
  for (let i = 0; i < pageSize; i++) {
    const file = randomFile()
    result.push({
      id: i + 1,
      url: file.url,
      path: file.path,
      filesize: file.filesize,
      mimetype: file.mimetype,
      ext: file.ext,
      configId: 1,
      folderId: Mock.mock('@natural(0, 19)'),
      name: Mock.mock('@ctitle'),
      alt: Mock.mock('@ctitle'),
      description: Mock.mock('@csentence()'),
      coverImageId: null,
      quote: Mock.mock('@natural(0, 1)'),
      quoteModule: null,
      quotePageId: null,
      width: file.width,
      height: file.height,
      duration: file.duration,
      creator: '1',
      create_time: getRandomDateTime(getFeatureDate(-10), getNowDate()),
      updater: '',
      update_time: '',
      deleted: false,
      deleted_time: ''
    })
  }
  return result
}

const randomFile = () => {
  const mimetypeList = ['image', 'video', 'audio', 'application', 'text']
  const imageExtList = ['png', 'jpg', 'jpeg', 'gif']
  const videoExtList = ['mp4', 'm3u8']
  const audioExtList = ['mp3']
  const applicationExtList = ['xlsx', 'zip', 'docx', 'pdf']
  const textExtList = ['html', 'css', 'js']
  const mimetype = mimetypeList[Mock.mock('@natural(0, 4)')]
  const domain = Mock.mock('@url()')
  let url = ''
  let filesize = 0
  let ext = ''
  let width = 0
  let height = 0
  let duration = 0
  const path = Mock.mock('@word(32)')
  if (mimetype === 'image') {
    ext = imageExtList[Mock.mock('@natural(0, 3)')]
    filesize = Mock.mock('@natural(100, 10485760)')
    width = Mock.mock('@natural(100, 1920)')
    height = Mock.mock('@natural(100, 1080)')
    const gcd = getGcd(width, height)
    url = Mock.Random.image(width + 'x' + height, Mock.mock('@color()'), '#FFF', ext, Mock.mock('@character()'))
  } else if (mimetype === 'video') {
    ext = videoExtList[Mock.mock('@natural(0, 1)')]
    filesize = Mock.mock('@natural(100, 52428800)')
    width = Mock.mock('@natural(100, 1920)')
    height = Mock.mock('@natural(100, 1080)')
    duration = Mock.mock('@natural(5, 600)')
    const gcd = getGcd(width, height)
    url = domain + '/' + path + '.' + ext
  } else if (mimetype === 'audio') {
    ext = audioExtList[0]
    filesize = Mock.mock('@natural(100, 52428800)')
    duration = Mock.mock('@natural(5, 600)')
    url = domain + '/' + path + '.' + ext
  } else if (mimetype === 'application') {
    ext = applicationExtList[Mock.mock('@natural(0, 3)')]
    filesize = Mock.mock('@natural(100, 104857600)')
    url = domain + '/' + path + '.' + ext
  } else {
    ext = textExtList[Mock.mock('@natural(0, 2)')]
    filesize = Mock.mock('@natural(100, 5242880)')
    url = domain + '/' + path + '.' + ext
  }
  return {
    url,
    path,
    ext,
    mimetype,
    filesize,
    width,
    height,
    duration
  }
}

// 计算最大公约数（尺寸比例）
const getGcd = (a, b) => {
  let n1, n2
  if (a > b) {
    n1 = a
    n2 = b
  } else {
    n1 = b
    n2 = a
  }
  const remainder = n1 % n2
  if (remainder === 0) {
    return n2
  } else {
    return getGcd(n2, remainder)
  }
}
